package com.zl.tby.service.impl;

import com.zl.tby.core.base.bean.TbyPageResult;
import com.zl.tby.core.base.util.ToolsUtil;
import com.zl.tby.core.common.R;
import com.zl.tby.dto.TbyWildCameraQuery;
import com.zl.tby.model.TbyWildCamera;
import com.zl.tby.repository.TbyWildCameraRepo;
import com.zl.tby.service.ITbyWildCameraService;
import com.zl.tby.vo.TbyWildCameraForm;
import org.noear.solon.annotation.Component;
import org.noear.solon.data.annotation.Tran;
import org.noear.wood.IPage;
import org.noear.wood.annotation.Db;
import org.noear.wood.utils.StringUtils;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 相机信息
 * @author changpeng
 * @version 1.0
 * @since 2024/8/6 20:22
 */
@Component
public class TbyWildCameraServiceImpl implements ITbyWildCameraService {

    @Db
    private TbyWildCameraRepo tbyWildCameraRepo;

    @Override
    @Tran
    public R<TbyWildCamera> add(TbyWildCameraForm form) {
        TbyWildCamera entity = form.createEntity();
        entity.setCreateBy(ToolsUtil.getLoginUserId());
        entity.setCreateTime(LocalDateTime.now());
        long id = tbyWildCameraRepo.insert(entity, true);
        entity.setId(id);
        return R.ok(entity);
    }

    @Override
    @Tran
    public R<TbyWildCamera> update(TbyWildCameraForm form) {
        TbyWildCamera entity = this.get(form.getId());
        if (entity == null) {
            return R.failed("数据不存在");
        }
        form.updateEntity(entity);
        entity.setUpdateBy(ToolsUtil.getLoginUserId());
        entity.setUpdateTime(LocalDateTime.now());
        tbyWildCameraRepo.updateById(entity, false);
        return R.ok(entity);
    }

    @Override
    public R<Void> delete(Long id) {
        // TODO 验证是否被引用
        int flag = tbyWildCameraRepo.deleteById(id);
        return flag > 0 ? R.ok() : R.failed("删除失败");
    }

    @Override
    public TbyWildCamera get(Long id) {
        return tbyWildCameraRepo.selectById(id);
    }

    @Override
    public TbyPageResult<TbyWildCamera> page(TbyWildCameraQuery query) {
        IPage<TbyWildCamera> iPage = tbyWildCameraRepo.selectPage(query.getOffset(), query.getSize(), mp -> {
            mp.whereTrue()
                    .andIf(StringUtils.isNotEmpty(query.getName()), "name like ?", "%" + query.getName() + "%")
                    .andIf(StringUtils.isNotEmpty(query.getCode()), "code = ?", query.getCode())
                    .andIf(StringUtils.isNotEmpty(query.getDeptId()), "dept_id = ?", query.getDeptId());
            if (StringUtils.isNotEmpty(query.getOrder())) {
                mp.orderBy(query.getOrder());
            }
        });
        return new TbyPageResult(iPage, query);
    }

    @Override
    public List<TbyWildCamera> list(TbyWildCameraQuery query) {
        return tbyWildCameraRepo.selectList(mapperWhereQ -> {
            mapperWhereQ.whereTrue().andIf(StringUtils.isNotEmpty(query.getName()), "name like ?", "%" + query.getName() + "%")
                    .andIf(StringUtils.isNotEmpty(query.getCode()), "code = ?", query.getCode())
                    .andIf(StringUtils.isNotEmpty(query.getDeptId()), "dept_id = ?", query.getDeptId());
            if (StringUtils.isNotEmpty(query.getOrder())) {
                mapperWhereQ.orderBy(query.getOrder());
            }
        });
    }

    @Override
    public R<Boolean> checkCode(Long id, String code) {
        if (StringUtils.isEmpty(code)) {
            return R.ok(false);
        }
        List<TbyWildCamera> dbList = this.tbyWildCameraRepo.selectList(mp -> {
            mp.whereEq("code", code);
        });
        for (TbyWildCamera data : dbList) {
            if (id == null && data.getCode().equals(code)) {
                return R.ok(true);
            } else if (id != null && data.getCode().equals(code) && id.compareTo(data.getId()) != 0) {
                return R.ok(true);
            }
        }
        return R.ok(false);
    }
}
